package com.sline.day01.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Demo2_DDL_Exception {
    public static void main(String[] args) {

    }

    // 创建一张t_student表，包含 id / name / age
    @Test
    public void createTable() throws Exception {
        String sql = "create table t_student(id bigint primary key auto_increment, name varchar(20), age int);";

        // 加载注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 获取连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

        // 创建/获取语句对象
        Statement statement = connection.createStatement();

        // 执行sql
        int rows = statement.executeUpdate(sql);
        System.out.println(rows + "");

        // 释放资源
        statement.close();
        connection.close();
    }

    // 正常处理资源和关闭资源
    @Test
    public void handleException() {
        String sql = "create table t_student(id bigint primary key auto_increment, name varchar(20), age int);";

        Connection connection = null;
        Statement statement = null;
        try {
            // 加载注册驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 获取连接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

            // 创建/获取语句对象
            statement = connection.createStatement();

            // 执行sql
            int rows = statement.executeUpdate(sql);
            System.out.println(rows + "");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (statement != null) {
                try {
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        try {
                            statement.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    // 正常处理资源和关闭资源
    @Test
    public void autoHandleByJava7() {
        String sql = "create table t_student(id bigint primary key auto_increment, name varchar(20), age int);";


        // 加载注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try (
            // 获取连接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

            // 创建/获取语句对象
            Statement statement = connection.createStatement();
        ) {
            // 执行sql
            int rows = statement.executeUpdate(sql);
            System.out.println(rows + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
